﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IRepository;

namespace CMS.Repository
{
    public class TeacherRepository : ITeacherRepository
    {
        private CMSEntities _connectObject;
        /// <summary>
        /// Initializes a new instance of the <see cref="TeacherRepository"/> class.
        /// </summary>
        public TeacherRepository()
        {
            _connectObject = new CMSEntities();

        }
        /// <summary>
        /// Selects all teacher.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="rowCount">The row count.</param>
        /// <returns></returns>
        public List<CMS.Model.Teacher> SelectAllTeacher(int pageIndex, int pageSize, out int rowCount)
        {
            var result = (from r in _connectObject.Teachers
                          select r).OrderBy(r => r.TeacherId).Skip((pageIndex - 1) * pageSize).Take(pageSize);

            rowCount = (from r in _connectObject.Teachers select r).ToList().Count;

            return Translator.TeacherTranslator.ToTeacherModeList(result.ToList());

        }
        /// <summary>
        /// Selects all teacher.
        /// </summary>
        /// <returns></returns>
        public List<CMS.Model.Teacher> SelectAllTeacher()
        {
            var result = from r in _connectObject.Teachers
                         select r;

            return Translator.TeacherTranslator.ToTeacherModeList(result.ToList());
        }

        /// <summary>
        /// Selects the teacher by id.
        /// </summary>
        /// <param name="teacherId">The teacher id.</param>
        /// <returns></returns>
        public List<CMS.Model.Teacher> SelectTeacherById(int teacherId)
        {
            var result = from r in _connectObject.Teachers
                         where r.TeacherId == teacherId
                         select r;

            return Translator.TeacherTranslator.ToTeacherModeList(result.ToList());

        }

        /// <summary>
        /// Inserts the teacher.
        /// </summary>
        /// <param name="teacherInstance">The teacher instance.</param>
        public void InsertTeacher(CMS.Model.Teacher teacherInstance)
        {
            List<CMS.Model.Teacher> tea = new List<CMS.Model.Teacher>();
            tea.Add(teacherInstance);
            List<CMS.Repository.Teacher> t = new List<CMS.Repository.Teacher>();
            t = Translator.TeacherTranslator.ToTeacherEFList(tea);
            t[0].Department = null;// otherwise the Department table is inserted!!!
            _connectObject.Teachers.AddObject(t[0]);
            _connectObject.SaveChanges();
        }

        /// <summary>
        /// Deletes the teacher by id.
        /// </summary>
        /// <param name="teacherId">The teacher id.</param>
        public void DeleteTeacherById(int teacherId)
        {

            var teachers = (from r in _connectObject.Teachers
                            where r.TeacherId == teacherId
                            select r).ToList();

            for (int i = 0; i < teachers.Count; i++)
            {
                int teacher_Id = teachers[i].TeacherId;
                var courses = (from a in _connectObject.Courses
                               where a.TeacherId == teacher_Id
                               select a).ToList();
                for (int j = 0; j < courses.Count; j++)
                {
                    int course_Id = courses[j].CourseId;
                    var courseDetails = (from b in _connectObject.CourseDetails
                                         where b.CourseId == course_Id
                                         select b).ToList();
                    for (int z = 0; z < courseDetails.Count; z++)
                    {
                        _connectObject.CourseDetails.DeleteObject(courseDetails[z]);
                    }
                    _connectObject.Courses.DeleteObject(courses[j]);
                }
                _connectObject.Teachers.DeleteObject(teachers[i]);
            }


            _connectObject.SaveChanges();
        }

        /// <summary>
        /// Updates the teacher.
        /// </summary>
        /// <param name="teacherInstance">The teacher instance.</param>
        public void UpdateTeacher(CMS.Model.Teacher teacherInstance)
        {
            var result = from r in _connectObject.Teachers
                         where r.TeacherId == teacherInstance.TeacherId
                         select r;
            foreach (var p in result)
            {
                p.TeacherName = teacherInstance.TeacherName;
                p.TeacherGender = teacherInstance.TeacherGender;
                p.DepartmentId = teacherInstance.DepartmentId;
                p.Department = new CMS.Repository.Department()
                {
                    DepartmentId = teacherInstance.Department.DepartmentId,
                    DepartmentName = teacherInstance.Department.DepartmentName,
                    Phone = teacherInstance.Department.Phone,
                    Manager = teacherInstance.Department.Manager,
                    Remark = teacherInstance.Department.Remark

                };

            }
            _connectObject.SaveChanges();
        }
    }
}
